import { createApp } from "vue";
import App from "./App.vue";
import store from "./store";
import router from "./router";
import axios from "axios";
import "easymde/dist/easymde.min.css"; // 导入easymde的样式文件

const app = createApp(App);

// 添加baseURL
axios.defaults.baseURL = "https://apis.imooc.com/api/";
// axios拦截器 request部分是在发送请求之前的内容 response部分用于处理回应内容
axios.interceptors.request.use(config => {
  config.params = { ...config.params, icode: "6484223442E894C2" };
  //   其他请求添加到body中
  // 上传文件添加到formdata
  if (config.data instanceof FormData) {
    // console.log("formdata");
    config.data.append("icode", "6484223442E894C2");
  } else {
    // 普通的body对象 添加到data中
    config.data = { ...config.data, icode: "6484223442E894C2" };
  }
  store.commit("setError", { status: false, message: "" }); // 置为false，多次请求的时候依旧会提示错误框
  store.commit("setLoading", true);
  return config;
});
axios.interceptors.response.use(
  config => {
    store.commit("setLoading", false);
    return config;
  },
  e => {
    const { error } = e.response.data;
    store.commit("setError", { status: true, message: error });
    store.commit("setLoading", false);
    return Promise.reject(e.response.data);
  }
);

// if (!window.vuerouter) app.use(router)
app.use(router);
app.use(store);
app.mount("#app");
